<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/gruppenkinder.php";
require_once $basedir."/inc/kinder.php";
require_once $basedir."/inc/vertraege.php";
$vertraege = Vertraege::getAll();
$gruppen = Gruppen::getAll();
if(isset($_REQUEST['save'])) {
    for($i=0; $i<sizeof($_REQUEST["id"]); $i++) {
        $gruppenkinder = new GruppenKinder();
        if($_REQUEST['id'][$i] != "new") {
            $gruppenkinder = new GruppenKinder($_REQUEST['id'][$i]);
        }
        $gruppenkinder->setGruppenId($_REQUEST['gruppe_id'][$i]);
        $gruppenkinder->setKindId($_REQUEST['kind_id'][$i]);
        if($gruppenkinder->getGruppenId()!="" && $gruppenkinder->getKindId()!="") {
            $gruppenkinder->save();
        }
    }
}
?>
<form id='gruppen_einteilung_set_form' method="post" action="/index.php">
    <input type="hidden" name="page" value="<?php echo $menuid;?>" />
    <table>
        <tr>
            <th></th>
            <th>Kind</th>
            <th>Gruppe</th>
        </tr>
<?php
for($i=0; $i<sizeof($vertraege);$i++) {
    $kind = $vertraege[$i]->getKind();
    $gruppenzuordnung = $kind->getGruppenzuordnung();
    $gruppe = "";
    $gruppenzuordnung_id = "new";
    $gruppen_id = "";
    if($gruppenzuordnung) {
        $gruppenzuordnung = $gruppenzuordnung[0];
        $gruppe = $gruppenzuordnung->getGruppe();
        $gruppenzuordnung_id = $gruppenzuordnung->getId();
        $gruppen_id = $gruppe->getId();
    }
?>
        <tr>
            <td><? echo $i; ?></td>
            <td><? echo $kind->getVorname()." ".$kind->getNachname(); ?></td>
            <td>
                <input type='hidden' name='id[]' value='<? echo $gruppenzuordnung_id; ?>' />
                <input type='hidden' name='kind_id[]' value='<? echo $kind->getId(); ?>' />
                <select name='gruppe_id[]'>
                    <option value='none'></option>
<?php
        for($x=0;$x<sizeof($gruppen);$x++) {
            $selected = "";
            if($gruppen[$x]->getId() == $gruppen_id) {
                $selected = "selected ";
            }
?>
                    <option value='<? echo $gruppen[$x]->getId(); ?>' <? echo $selected; ?>><? echo $gruppen[$x]->getName(); ?></option>
<?php
        }
?>
                </select>
            </td>
        </tr>
<?php
}
?>
    </table>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
